home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 4.4 KB | 134 lines | [TEXT/MPS ] |
- // UPatch.h
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
-
- #ifndef __UPATCH__
- #define __UPATCH__
-
- //----------------------------------------------------------------------------------------
- // Theory of Operation
- //
- // This unit implements a general trap-patching mechanism.
- //
- // The trap is patched such that it jumps to the routine of your choice, ignoring the
- // routine it previously jumped to. On a 128K ROM machine, the patch is made by setting
- // the trap address to your routine. On a 64K machine, a small block of code is
- // allocated in the system heap:The trap address is set to point to the block, and the
- // block contains code to jump to your routine.
- //
- // References to patches are kept in TrapPatch records. For each trap patched you must
- // define a variable of type TrapPatch. This unit links the TrapPatch records to form a
- // linked list to facilitate unpatching all patches without specifying each individual
- // patch.
- //----------------------------------------------------------------------------------------
-
- // MacApp
-
- #ifndef __MACAPPTYPES__
- #include "MacAppTypes.h"
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // TrapPatch: Preferred. The pointer type _MUST_ be declared first since the record is
- // self referential
- //----------------------------------------------------------------------------------------
-
- typedef struct TrapPatch *TrapPatchPtr;
-
- struct TrapPatch
- {
- protected:
- short trapNum; // trap # being patched
- UniversalProcPtr oldTrapAddr; // old trap address
- UniversalProcPtr patchRoutine; // new trap routine descriptor (created by subclasses
- // of class TrapPatch, cleaned up by TrapPatch)
- TrapPatchPtr nextPatch; // next link in linked list of patches
-
- public:
- TrapPatch();
-
- short PatchTrap(short theTrapNum, void* theRoutine);
- // PatchTrap patches the given trap to theRoutine.
-
- void LookupOldTrapAddress(short theTrapNum);
-
- TrapPatchPtr GetPreviousPatchPtr();
- TrapPatchPtr GetNewerPatchPtr();
- void UnpatchTrap();
- // UnpatchTrap unpatches the given trap.
-
- static void UnpatchAll();
- // UnpatchAll unpatches all traps.
-
- UniversalProcPtr GetOldTrapAddr() { return oldTrapAddr; }
-
- protected:
- //----------------------------------------------------------------------------------------
- // The following variable is really private but is in the interface just in case you're
- // doing something really weird and you really need it
- //----------------------------------------------------------------------------------------
-
- static TrapPatchPtr pPatchList;
- };
-
- #if !qPowerPC
- //----------------------------------------------------------------------------------------
- // CallBack
- //----------------------------------------------------------------------------------------
-
- struct CallBack
- {
- short saveRtnAdd; // Internal use
- short moveRefCon; // Internal use
- long refCon; // the refCon that will be passed as last
- // parm
- short targOffset; // Internal use
- short jmpInst; // Internal use
- void* jmpTarg; // Internal use
- };
-
- typedef CallBack* CallBackPtr;
-
- #endif
-
- #if !qPowerPC
- //----------------------------------------------------------------------------------------
- // JmpInstructionTemplate: template for creating/patching JMP instructions.
- //----------------------------------------------------------------------------------------
-
- struct JmpInstructionTemplate {
- short Jmp; // jmp instruction
- void* Routine; // address to jump to
- };
-
- #endif
-
- //----------------------------------------------------------------------------------------
- // External function declarations
- //----------------------------------------------------------------------------------------
-
- void FlushCache();
- // Flush the data and instruction cache.
-
- #if !qPowerPC
- void PatchJmpInstruction(void* patchAddress, void* jumpAddress);
- // Place a 'jmp jumpAddress' instruction at patchAddress.
- #endif
-
- #if !qPowerPC
- void SetCallBack(void* targProc, long itsRefCon, CallBackPtr theCallBackPtr);
- // Prepares a call back record for use. Stuffs in the target of the call back and the
- // refcon to pass as an _additional_ and _last_ parameter to it. Useful for getting
- // context when being called back from the toolBox. Also useful for avoiding use of
- // globals when getting context.
- #endif
-
- inline void UnpatchAll()
- {
- TrapPatch::UnpatchAll();
- }
- // UnpatchAll unpatches all traps.
-
-
- #endif
-